import json
from ssd1306 import SSD1306_I2C
from machine import Pin, I2C

# 定义时钟引脚，
scl = Pin(5)
# 定义数据针脚
sda = Pin(4)
# 初始化一个I2C对象
i2c = I2C(scl=scl, sda=sda)
# 使用I2C对象初始化一个SSD1306屏幕驱动对象
oled = SSD1306_I2C(128, 64, i2c)

def show_loading():
    oled.fill(0)
    oled.text("loading...", 32, 24)
    oled.show()

def show_oled(text):
    # 让屏幕所有像素点清零
    oled.fill(0)
    # 在x轴是0，Y轴是0的位置开始显示英文，也就是左上角开始显示
    # oled.text(txt, 24, 16)
    # 显示第二行文字，英文一个字母，纵向，Y轴 占据8个像素点，这里我们为了好看，我们在偏移8个像素，从16行开始，
    # 相当于我们中间有个空行，
    # oled.text("I love you", 16, 16)
    # 同上面的道理我们显示设备的IP地址,需要前面链接上网络
    # 　wlan = network.WLAN(network.STA_IF)
    # oled.text("whole life", 32, 32)
    # oled.text("ip:"+wlan.ifconfig()[0], 0, 32)
    # 这里显示我么脚注，我们向右偏移64个像素点进行显示
    # oled.text("By kelebb", 56, 50)
    # 显示我们上面说有的设置
    chinese('温度：' + str(text["temperature"]) + '℃', 8, 0)
    chinese('湿度：' + str(text["humidity"]) + '%', 8, 32)
    oled.show()


def chinese(ch_str, x_axis, y_axis):
    offset_ = 0
    y_axis = y_axis + 8  # 中文高度一行占8个
    x_axis = (x_axis + 16)  # 中文宽度占16个
    for k in ch_str:
        byte_data = fontJson.get(k)
        if len(byte_data) > 16:
            for y in range(0, 16):
                a_ = bin(byte_data[y]).replace('0b', '')
                while len(a_) < 8:
                    a_ = '0' + a_
                b_ = bin(byte_data[y + 16]).replace('0b', '')
                while len(b_) < 8:
                    b_ = '0' + b_
                for x in range(0, 8):
                    oled.pixel(x_axis + offset_ + x, y + y_axis, int(a_[x]))
                    oled.pixel(x_axis + offset_ + x + 8, y + y_axis, int(b_[x]))
            offset_ += 16
        else:
            for y in range(0, 16):
                a_ = bin(byte_data[y]).replace('0b', '')
                while len(a_) < 8:
                    a_ = '0' + a_
                b_ = ''
                if y < 8:
                    b_ = bin(byte_data[y + 8]).replace('0b', '')
                    while len(b_) < 8:
                        b_ = '0' + b_
                for x in range(0, 8):
                    oled.pixel(x_axis + offset_ + x, y + y_axis, int(a_[x]))
                    if x < 4 and b_ != '':
                        oled.pixel(x_axis + offset_ + x + 4, y + y_axis, int(b_[x]))
            offset_ += 8


def chinese_fonts(ch_str, x_axis, y_axis):
    offset_ = 0
    y_axis = y_axis * 8  # 中文高度一行占8个
    x_axis = (x_axis * 16)  # 中文宽度占16个
    for k in ch_str:
        code = 0x00  # 将中文转成16进制编码
        data_code = k.encode("utf-8")
        code |= data_code[0] << 16
        code |= data_code[1] << 8
        code |= data_code[2]
        byte_data = fonts[code]
        print(k)
        print(byte_data)
        print(len(byte_data))
        for y in range(0, 16):
            a_ = bin(byte_data[y]).replace('0b', '')
            while len(a_) < 8:
                a_ = '0' + a_
            b_ = bin(byte_data[y + 16]).replace('0b', '')
            while len(b_) < 8:
                b_ = '0' + b_
            for x in range(0, 8):
                oled.pixel(x_axis + offset_ + x, y + y_axis, int(a_[x]))
                oled.pixel(x_axis + offset_ + x + 8, y + y_axis, int(b_[x]))
                oled.show()
        offset_ += 16


fontJson = {
    "温":
        [0x00, 0x23, 0x12, 0x12, 0x83, 0x42, 0x42, 0x13, 0x10, 0x27, 0xE4, 0x24, 0x24, 0x24, 0x2F, 0x00, 0x00, 0xF8,
         0x08, 0x08, 0xF8, 0x08, 0x08, 0xF8, 0x00, 0xFC, 0xA4, 0xA4, 0xA4, 0xA4, 0xFE, 0x00],  # "温",0*/
    "湿":
        [0x00, 0x27, 0x14, 0x14, 0x87, 0x44, 0x44, 0x17, 0x11, 0x21, 0xE9, 0x25, 0x23, 0x21, 0x2F, 0x00, 0x00, 0xF8,
         0x08, 0x08, 0xF8, 0x08, 0x08, 0xF8, 0x20, 0x20, 0x24, 0x28, 0x30, 0x20, 0xFE, 0x00],  # "湿",1*/
    "度":
        [0x01, 0x00, 0x3F, 0x22, 0x22, 0x3F, 0x22, 0x22, 0x23, 0x20, 0x2F, 0x24, 0x42, 0x41, 0x86, 0x38, 0x00, 0x80,
         0xFE, 0x20, 0x20, 0xFC, 0x20, 0x20, 0xE0, 0x00, 0xF0, 0x10, 0x20, 0xC0, 0x30, 0x0E],  # "度",2*/
    "：":
        [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],  # "：",3*/
    "℃":
        [0x60, 0x91, 0x96, 0x6C, 0x08, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x08, 0x0C, 0x06, 0x01, 0x00, 0x00, 0xF4,
         0x0C, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x08, 0xF0, 0x00],  # "℃",4*/
    "%":
        [0x00, 0x00, 0x00, 0x44, 0xA4, 0xA8, 0xA8, 0xB0, 0x54, 0x1A, 0x2A, 0x2A, 0x4A, 0x44, 0x00, 0x00],  # "%",5*/
    "0":
        [0x00, 0x00, 0x00, 0x18, 0x24, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x24, 0x18, 0x00, 0x00],  # "0",6*/
    "1":
        [0x00, 0x00, 0x00, 0x08, 0x38, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x3E, 0x00, 0x00],  # "1",7*/
    "2":
        [0x00, 0x00, 0x00, 0x3C, 0x42, 0x42, 0x42, 0x02, 0x04, 0x08, 0x10, 0x20, 0x42, 0x7E, 0x00, 0x00],  # "2",8*/
    "3":
        [0x00, 0x00, 0x00, 0x3C, 0x42, 0x42, 0x02, 0x04, 0x18, 0x04, 0x02, 0x42, 0x42, 0x3C, 0x00, 0x00],  # "3",9*/
    "4":
        [0x00, 0x00, 0x00, 0x04, 0x0C, 0x0C, 0x14, 0x24, 0x24, 0x44, 0x7F, 0x04, 0x04, 0x1F, 0x00, 0x00],  # "4",10*/
    "5":
        [0x00, 0x00, 0x00, 0x7E, 0x40, 0x40, 0x40, 0x78, 0x44, 0x02, 0x02, 0x42, 0x44, 0x38, 0x00, 0x00],  # "5",11*/
    "6":
        [0x00, 0x00, 0x00, 0x18, 0x24, 0x40, 0x40, 0x5C, 0x62, 0x42, 0x42, 0x42, 0x22, 0x1C, 0x00, 0x00],  # "6",12*/
    "7":
        [0x00, 0x00, 0x00, 0x7E, 0x42, 0x04, 0x04, 0x08, 0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00],  # "7",13*/
    "8":
        [0x00, 0x00, 0x00, 0x3C, 0x42, 0x42, 0x42, 0x24, 0x18, 0x24, 0x42, 0x42, 0x42, 0x3C, 0x00, 0x00],  # "8",14*/
    "9":
        [0x00, 0x00, 0x00, 0x38, 0x44, 0x42, 0x42, 0x42, 0x46, 0x3A, 0x02, 0x02, 0x24, 0x18, 0x00, 0x00]  # "9",15*/
}

fonts = {
    0xe585b3:
        [0x10, 0x08, 0x08, 0x00, 0x3F, 0x01, 0x01, 0x01, 0xFF, 0x01, 0x02, 0x02, 0x04, 0x08, 0x30, 0xC0,
         0x10, 0x10, 0x20, 0x00, 0xF8, 0x00, 0x00, 0x00, 0xFE, 0x00, 0x80, 0x80, 0x40, 0x20, 0x18, 0x06],  # 关",0
    0xe788b1:
        [0x00, 0x01, 0x7E, 0x22, 0x11, 0x7F, 0x42, 0x82, 0x7F, 0x04, 0x07, 0x0A, 0x11, 0x20, 0x43, 0x1C,
         0x08, 0xFC, 0x10, 0x10, 0x20, 0xFE, 0x02, 0x04, 0xF8, 0x00, 0xF0, 0x10, 0x20, 0xC0, 0x30, 0x0E],  # 爱",1
    0xe58d95:
        [0x10, 0x08, 0x04, 0x3F, 0x21, 0x21, 0x3F, 0x21, 0x21, 0x3F, 0x01, 0x01, 0xFF, 0x01, 0x01, 0x01,
         0x10, 0x20, 0x40, 0xF8, 0x08, 0x08, 0xF8, 0x08, 0x08, 0xF8, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00],  # 单",2
    0xe8baab:
        [0x02, 0x04, 0x1F, 0x10, 0x1F, 0x10, 0x1F, 0x10, 0x10, 0x7F, 0x00, 0x00, 0x03, 0x1C, 0xE0, 0x00,
         0x00, 0x00, 0xF0, 0x10, 0xF0, 0x10, 0xF2, 0x14, 0x18, 0xF0, 0x50, 0x90, 0x10, 0x10, 0x50, 0x20],  # 身",3
    0xe78b97:
        [0x00, 0x44, 0x29, 0x11, 0x2A, 0x4C, 0x89, 0x09, 0x19, 0x29, 0x49, 0x89, 0x08, 0x08, 0x50, 0x20,
         0x80, 0x80, 0x00, 0xFC, 0x04, 0x04, 0xE4, 0x24, 0x24, 0x24, 0xE4, 0x24, 0x04, 0x04, 0x28, 0x10],  # 狗",4
    0xe68890:
        [0x00, 0x00, 0x00, 0x3F, 0x20, 0x20, 0x20, 0x3E, 0x22, 0x22, 0x22, 0x22, 0x2A, 0x44, 0x40, 0x81,
         0x50, 0x48, 0x40, 0xFE, 0x40, 0x40, 0x44, 0x44, 0x44, 0x28, 0x28, 0x12, 0x32, 0x4A, 0x86, 0x02],  # 成",5
    0xe995bf:
        [0x08, 0x08, 0x08, 0x08, 0x08, 0x09, 0x08, 0xFF, 0x0A, 0x09, 0x08, 0x08, 0x09, 0x0A, 0x0C, 0x08,
         0x00, 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x80, 0x40, 0x20, 0x18, 0x06, 0x00],  # 长",6
    0xe58d8f:
        [0x20, 0x20, 0x20, 0x20, 0xFB, 0x20, 0x20, 0x22, 0x22, 0x24, 0x28, 0x20, 0x21, 0x21, 0x22, 0x24,
         0x80, 0x80, 0x80, 0x80, 0xF0, 0x90, 0x90, 0x98, 0x94, 0x92, 0x92, 0x90, 0x10, 0x10, 0x50, 0x20],  # 协",7
    0xe4bc9a:
        [0x01, 0x01, 0x02, 0x04, 0x08, 0x30, 0xCF, 0x00, 0x00, 0x7F, 0x02, 0x04, 0x08, 0x10, 0x3F, 0x10,
         0x00, 0x00, 0x80, 0x40, 0x20, 0x18, 0xE6, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x20, 0x10, 0xF8, 0x08]  # 会",8
}
